import request from 'umi-request';
import Cookies from 'js-cookie';
import { ServiceTabKey } from './utils/consts';

export interface IUser {
  [x: string]: any;
  departmentname: string;
  deptid: number;
  id: string;
  identityid: number;
  name: string;
  useremail: string;
  usergender: 1 | 2;
  userloginname: string;
  usermobile: string;
  userrealname: string;
  usertelephone: string;
}

export interface IDept {
  [x: string]: any;
  deptname: string;
  id: string;
  identityid: number;
  name: string;
  deptpathname: string;
}

export type IMember = IUser | IDept | IUserGroup | IPosition;

export interface IMemberRes<T> {
  Total: number;
  Index: number;
  PageCount: number;
  PageSize: number;
  Info: T;
}

export interface IUserGroup {
  [x: string]: any;
  id: string;
  grouptype: number;
  identityid: number;
  name: string;
}

export interface IUserGroupServers {
  [x: string]: any;
  pageIndex: number;
  pageSize: number;
  total: number;
  rows: string;
}

export interface IPosition {
  [x: string]: any;
  id: string;
  identityid: number;
  name: string;
  level: number;
  positionname: string;
  positioncode: string;
  deptpathname?: string;
}

export interface IService {
  [x: string]: any;
  getUserList: (args: {
    departmentId: number;
    searchContent: string;
    isChild: boolean;
    pageNum: number;
    pageSize: number;
  }) => Promise<IMemberRes<IUser[]>>;
  getTopDepartmentList: (args: { departmentIds: number[] }) => Promise<IMemberRes<IDept[]>>;
  getSubDepartmentList: (args: { departmentId: number; id: string }) => Promise<IDept[]>;
  getDepartmentList: (args: {
    departmentId: number;
    searchContent: string;
    isChild: boolean;
    pageNum: number;
    pageSize: number;
    isContainSelf?: boolean;
  }) => Promise<IMemberRes<IDept[]>>;
  getUserGroupList: (args: {
    pageIndex?: Number;
    pageSize?: Number;
  }) => Promise<IUserGroupServers>;
  searchUserGroupList: (args: {
    groupName?: string;
    pageIndex?: Number;
    pageSize?: Number;
  }) => Promise<IUserGroupServers>;
  getTopPositionList: (args: { positionIds: number[] }) => Promise<IPosition[]>;
  getSubPositionList: (args: { positionId: number; id: string }) => Promise<IPosition[]>;
  getPositionList: (args: {
    positionId: number;
    searchContent: string;
    isChild: boolean;
    pageNum: number;
    pageSize: number;
    isContainSelf?: boolean;
  }) => Promise<IMemberRes<IPosition[]>>;
  getPositionListByKetword: (keyword: string) => Promise<any>;
  search: (args: {
    topNum: number;
    searchContent: string;
    searchDeptId: string;
    searchPositionId: string;
    // searchGroupId: string;
    tabs: any;
    // isGroupInfo: boolean;
  }) => Promise<{
    DeptList: IDept[];
    GroupList: IUserGroup[];
    PositionList: IPosition[];
    UserList: IUser[];
    MemberList: (IDept | IUserGroup | IPosition | IUser)[];
  }>;
}

const service: IService = {
  getUserList(args) {
    const data = {
      AllowDept: true,
      AllowPosition: true,
      AllowGroup: true,
      AllowUser: true,
      AllowMyGroup: false,
      AllowMyContact: false,
      type: true,
      HideEveryone: 1,
      SearchContent: args.searchContent || '',
      IsChild: args.isChild || false,
      DeptIds: args.departmentId,
      IsLike: true,
      page: args.pageNum,
      rows: args.pageSize,
    };
    const params = {
      token: Cookies.get('token'),
      index: args.pageNum,
      size: args.pageSize,
      dataFilter: 9,
    };
    return request(`/org/api/UserRead/SearchUserListPagedNew`, {
      method: 'POST',
      requestType: 'form',
      params,
      data: data,
    });
  },
  getUserGroupList(params: { pageIndex?: Number; pageSize?: Number }) {
    return request(
      `/org/api/GroupRead/GetGroupPages`,
      {
        params: {
          token: Cookies.get('token'),
          dataFilter: 2,
          pageIndex: params.pageIndex || 1,
          pageSize: params.pageSize,
          ...params,
        },
      },
    );
  },
  searchUserGroupList(params: { groupName?: string; pageIndex?: Number; pageSize?: Number }) {
    return request(
      `/org/api/GroupRead/GetGroupsByNamePage`,
      {
        params: {
          token: Cookies.get('token'),
          dataFilter: 2,
          groupName: params.groupName,
          pageIndex: params.pageIndex || 1,
          pageSize: params.pageSize,
          ...params,
        },
      },
    );
  },
  getGroupInfoByIds(groupIds: any) {
    return request(`/api/services/orgUserGroup/GetGroupInfoByIds`, {
      params: {
        token: Cookies.get('token'),
        groupIds,
      },
    });
  },
  getTopPositionList(params: { positionIds: string | number[] }) {
    let positionIds = typeof params.positionIds == 'string' ? params.positionIds.indexOf('1') != -1 ? '1' : params.positionIds : params.positionIds.includes(1) ? '1' : params.positionIds.join(',');
    return request(`/org/api/PositionRead/GetPositionInfoByIdentityIds`, {
      params: {
        token: Cookies.get('token'),
        positionIds: positionIds,
      },
    });
  },
  getSubPositionList(params) {
    return request(`/org/api/PositionRead/GetChildPositionListByIdentityId`, {
      params: {
        token: Cookies.get('token'),
        positionId: params.positionId,
        id: params.id,
      },
    });
  },
  getTopDepartmentList(params: { departmentIds: string | number[] }) {
    let departmentIds = typeof params.departmentIds == 'string' ? params.departmentIds.indexOf('1') != -1 ? '1' : params.departmentIds : params.departmentIds.includes(1) ? '1' : params.departmentIds.join(',');
    return request(`/org/api/DeptRead/GetDepartmentInfoByIdentityIds`, {
      params: {
        token: Cookies.get('token'),
        deptIds: departmentIds,
      },
    });
  },
  getDepartmentInfoById(deptId: any) {
    return request(`/api/services/OrgDepartment/GetDepartmentInfoById`, {
      params: {
        token: Cookies.get('token'),
        deptId,
      },
    });
  },
  getSubDepartmentList(params) {
    return request(`/org/api/DeptRead/GetChildDepartmentListByIdentityId`, {
      params: {
        token: Cookies.get('token'),
        deptId: params.departmentId,
        id: params.id,
      },
    });
  },
  getDepartmentList(params) {
    return request(`/org/api/DeptRead/SearchDeptListPaged`, {
      method: 'POST',
      requestType: 'form',
      params: {
        token: Cookies.get('token'),
        [params.pageNum]: params.pageSize,
      },
      data: {
        isContainSelf: !!params.isContainSelf,
        Account: '',
        RealName: '',
        IsChild: params.isChild,
        IsLike: true,
        SearchContent: params.searchContent,
        AllowDept: true,
        AllowPosition: true,
        AllowGroup: true,
        AllowUser: true,
        page: params.pageNum,
        rows: params.pageSize,
        ...(params.departmentId
          ? {
            AllowMyGroup: false,
            AllowMyContact: false,
            type: true,
            HideEveryone: 1,
            DeptIds: params.departmentId,
          }
          : {}),
      },
    });
  },
  getPositionList(args) {
    const data = {
      AllowDept: true,
      AllowPosition: true,
      AllowGroup: true,
      AllowUser: true,
      AllowMyGroup: false,
      AllowMyContact: false,
      type: true,
      HideEveryone: 1,
      SearchContent: args.searchContent || '',
      IsChild: args.isChild || false,
      positionIds: args.positionId,
      IsLike: true,
      page: args.pageNum,
      rows: args.pageSize,
      isContainSelf: !!args.isContainSelf,
    };
    const params = {
      token: Cookies.get('token'),
      [args.pageNum]: args.pageSize,
    };
    return request(`/org/api/PositionRead/SearchPositionListPaged`, {
      method: 'POST',
      requestType: 'form',
      params,
      data: data,
    });
  },
  getUserGroupUserList(params: { groupId: string; page: number; rows: number; userNme: string }) {
    const { groupId, page = 1, rows = 10, userNme = '' } = params;
    return request(`/org/Admin/Group/SearchUserInGroupPageNew`, {
      params: {
        organizationType: -1,
        token: Cookies.get('token'),
        groupId,
        page,
        rows,
        userNme,
      },
    });
  },
  getUserGroupAndDeptUserList(params: {
    deptId: string;
    groupId: string;
    userName: string;
    pageIndex: number;
    pageSize: number;
  }) {
    const { deptId, groupId, userName, pageIndex = 1, pageSize = 10 } = params;
    return request(`/api/services/OrgDepartment/GetUserListByDeptIdAndGroupId`, {
      params: {
        token: Cookies.get('token'),
        deptId,
        groupId,
        userName,
        pageIndex,
        pageSize,
      },
    });
  },
  getUserInfoByUserId(params: { userIds: string; recursive?: Boolean }) {
    return request(`/api/services/OrgUser/GetAllUserInfoByUserId`, {
      params: {
        token: Cookies.get('token'),
        userIds: params.userIds,
        recursive: params.recursive || true,
      },
    });
  },
  getPositionListByKetword(keyword: string) {
    return request(`/api/services/OrgPosition/GetPositionInfoListByKeyword`, {
      params: {
        token: Cookies.get('token'),
        keyword: keyword,
      },
    });
  },
  search(args) {
    return request('/Component', {
      method: 'POST',
      requestType: 'form',
      data: {
        module: 'ComponentClient',
        fun: 'InbizSearch',
        topNum: args.topNum,
        'deptIds[]': '',
        'positionIds[]': '',
        'groupIds[]': '',
        account: '',
        realName: '',
        isChild: true,
        isLike: true,
        searchContent: args.searchContent,
        allowTypes: args.tabs,
        offUser: false,
        searchDeptId: args.searchDeptId,
        searchPositionId: args.searchPositionId,
        // searchGroupId: args.searchGroupId,
        // isGroupInfo: args.isGroupInfo,
      },
    });
  },
};

export default service;
